<?php

/**
 * Class for rewriting URL
 *
 * @package OWL
 * @subpackage Helper
 * @since 1.0
 * @author KeiMOon
 */

class Owl_Rewrite {
    /**
     * Array of rewrite rules
     *
     * @var Array $rules Array of rewrite rules and its priority
     * @access private
     */
    private $rules;

    /**
     * Rewrite engine is enabled or not
     *
     * @var bool
     */
    private $enabled;

    /**
     * Constructer function, PHP 5 style.<br />
     * Register default rules
     *
     * @access public
     */
    function  __construct() {
        register_shutdown_function(array(&$this,"__destruct"));
        $this->rules = array();
        $this->enabled = REWRITE_ENABLE;
    }

    /**
     * PHP 4 constructer function.
     *
     * @access public
     */
    function Owl_Rewrite() {
        return $this->__construct();
    }

    /**
     * Destructer function
     *
     * @return true Always return true
     */
    function  __destruct() {
        return true;
    }

    /**
     * Add a rule
     *
     * @access public
     * @param string $pattern
     * @parem string $replacement
     * @param int $priority
     * @return bool True if success.
     */
    function add_rule($pattern, $replacement, $priority) {
        foreach ($this->rules as $rule) {
            if ($rule["pattern"] == $pattern && $rule["replacement"] == $replacement) return false;
        }
        $this->rules[] = array("pattern"=>$pattern,"replacement"=>$replacement,"priority"=>$priority);
        return true;
    }

    /**
     * Sort rule based on priority 
     * 
     * @access public
     */
    function sort_rule() {
        $the_rule = array();
        $priority = array();
        foreach ($this->rules as $rule) {
            $the_rule[] = $rule["the_rule"];
            $priority[] = $rule["priority"];
        }
        array_multisort($priority, SORT_ASC, $this->rules);
    }

    /**
     * Apply the rewrite rule to a string
     *
     * @param string $string
     * @return string 
     */
    function apply_rules($string) {
        if (!$this->enabled || !getenv('OWL_REWRITE_ENV')) {
            return $string;
        }
        $this->sort_rule();
        foreach ($this->rules as $rule) {
            $pattern = $rule["pattern"];
            $replacement = $rule["replacement"];
            if (preg_match($pattern, $string)) {
                return preg_replace($pattern, $replacement, $string);;
            }
        }
        return $string;
    }

    function enable_rewrite_engine($enable = true) {
        $this->enabled = $enable;
    }

}

//API - Old style singleton because this function is used a lots

/**
 * Add a rewrite rule
 *
 * @param string $pattern
 * @param string $replacement
 * @param int $priority
 * @return bool
 */
function add_rewrite_rule($pattern, $replacement, $priority) {
    global $rewrite_engine;
    return $rewrite_engine->add_rule($pattern, $replacement, $priority);
}

/**
 * Apply the rewrite rule to a string
 *
 * @param string $string
 * @return string
 */
function apply_rewrite_rules($string) {
    global $rewrite_engine;
    return $rewrite_engine->apply_rules($string);
}
//Default rewrite rules
function register_default_rules() {
    add_rewrite_rule('/^index.php\?template=([\w\-]+)&var=([\w\-]+)$/i', '${1}/${2}.html', 1);
    add_rewrite_rule('/^index.php\?template=([\w\-]+)$/i', '${1}.html', 2);
}

